<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增图书')" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form enctype="multipart/form-data" class="form-horizontal m" id="form-book-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">图书编号：</label>
                <div class="col-sm-8">
                    <input id="bookNo" name="bookNo" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">图书名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图书图片：</label>
                <div class="col-sm-8">
                    <div id="screen" class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;">
                        </div>
                        <div>
                        <span class="btn btn-white btn-file">
                            <span class="fileinput-new">选择图片</span><span class="fileinput-exists">更改</span>
                            <input name="picture" type="file" id="inputImage" accept="image/*"></span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">图书介绍：</label>
                <div class="col-sm-8">
                    <textarea name="introduce" maxlength="100"  class="form-control" type="text" placeholder="请输入图书简介"></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">价格：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select class="form-control m-b" name="typeId" id="selectType"></select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script th:inline="javascript">
        $(function() {
            //添加图片逻辑
            $('#screen').on('change.bs.fileinput', function (e) {
                var file = $('#inputImage')[0].files[0];
                if (!/^image\/\w+$/.test(file.type)) {
                    $.modal.alertWarning('请选择一个图片文件。');
                    $('#screen').fileinput('reset');
                }
            });

            //类型下拉框初始化
            let view = $('#selectType');
            $.ajax({
                url:ctx + 'admin/type/listNotPage',
                type:'post',
                dataType:'json',
                success: function(result) {
                    result.rows.map( item => {
                        view.append('<option value="'+ item.typeId +'">'+ item.name +'</option>');
                    });
                }
            });
        });

        var prefix = ctx + "admin/book"

        $("#form-book-add").validate({
            onkeyup: false,
            rules: {
                price: {
                    number: true
                },
                bookNo: {
                    isUserName: true,
                    remote: {
                        url: prefix + "/checkBookNo",
                        type: "post",
                        dataType: "json",
                        data: {
                            "bookNo": function () {
                                return $.common.trim($("#bookNo").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "bookNo": {
                    remote: "图书编号已经存在"
                },
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var formdata = new FormData($('#form-book-add')[0]);
                $.ajax({
                    url:prefix + '/add',
                    type:'post',
                    data:formdata,
                    processData: false,
                    contentType: false,
                    dataType:'json',
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                        $.modal.disable();
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.successCallback(result);
                    }
                });
            }
        }
    </script>
</body>
</html>